package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.common.UserContext;
import org.wmq.travelagency02.model.Entity.Guidebook;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.GuidebookService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/guidebook")
@RequiredArgsConstructor
public class GuidebookController {
    @Autowired
    private GuidebookService guidebookService;

    @GetMapping("/{id}")
    public Result<Guidebook> getById(@PathVariable Long id){
        Guidebook result = guidebookService.getById(id);
        if (result != null) {
            return Result.ok(result);
        }
        return Result.error("获取失败");
    }

    @GetMapping("/me")
    public Result<Guidebook> me(){
        return this.getById(UserContext.getUserId());
    }

    @GetMapping("/getByName/{name}")
    public Result<Guidebook> getByName(@PathVariable String name){
        Guidebook result = this.guidebookService.lambdaQuery().
                eq(Guidebook::getName, name).
                select(Guidebook::getId,
                        Guidebook::getPhone, Guidebook::getAge,
                        Guidebook::getGender).one();
        if (result != null) {
            result.setName(name);
            return Result.ok(result);
        }
        return Result.error("获取失败");
    }

    @GetMapping("/getByPhone/{phone}")
    public Result<Guidebook> getByPhone(@PathVariable String phone){
        Guidebook result = this.guidebookService.lambdaQuery().
                eq(Guidebook::getPhone, phone).
                select(Guidebook::getId,
                        Guidebook::getName, Guidebook::getAge,
                        Guidebook::getGender).one();
        if (result != null) {
            result.setPhone(phone);
            return Result.ok(result);
        }
        return Result.error("获取失败");
    }

    @PostMapping
    public Result<Void> save(@RequestBody Guidebook guidebook){
        guidebook.setCreateTime(LocalDateTime.now());
        guidebook.setUpdateTime(LocalDateTime.now());
        if(guidebookService.save(guidebook)){
            return Result.ok();
        }
        return Result.error("保存失败");
    }

    @PutMapping
    public Result<Void> update(@RequestBody Guidebook guidebook){
        guidebook.setUpdateTime(LocalDateTime.now());
        if(guidebookService.updateById(guidebook)){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id){
        if(guidebookService.removeById(id)){
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @GetMapping("/getByChatUserId/{chatUserId}")
    public Result<Guidebook> getByChatUserId(@PathVariable String chatUserId){
        Guidebook result = this.guidebookService.lambdaQuery().
                eq(Guidebook::getChatUserId, chatUserId).
                select(Guidebook::getId,
                        Guidebook::getName, Guidebook::getPhone,
                        Guidebook::getAge, Guidebook::getGender).one();
        if (result != null) {
            result.setChatUserId(chatUserId);
            return Result.ok(result);
        }
        return Result.error("获取失败");
    }
}
